package tz.preprocessing.stopwords;

import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Map;

/**
 * 
 * @author dejvid
 * 
 *         Prebere stop words. Stop words morajo biti zapisani v tekstovni
 *         datoteki,kjer so besede med sabo ločene z vejico in ne sme vsebovati
 *         presledkov.
 * 
 */
public class StopWordsExtract
{
	ArrayList<String>	lstStopWords;
	
	public StopWordsExtract(File stopWords)
	{
		lstStopWords = new ArrayList<String>();
		try
		{
			FileReader fr = new FileReader(stopWords);
			String word = "";
			do
			{
				word = getNextStopWord(fr);
				if (word.compareTo("") != 0)
				{
					lstStopWords.add(word.toLowerCase());
					 //System.out.println("Stop word: " + word);
				}
			} while (word.compareTo("") != 0);
			
			fr.close();
		} catch (Exception e)
		{
			System.err.println("");
		}
	}
	
	public boolean isStopWord(String word)
	{
		return lstStopWords.contains(word.toLowerCase());
	}
	
	public String getNextStopWord(FileReader fr) throws Exception
	{
		int state = 0;
		int c = 0;
		StringBuffer word = new StringBuffer();
		while (state != -1)
		{
			switch (state)
			{
				case 0: // STATE 0
				{
					c = fr.read();
					if (c == -1)
					{
						state = -1; // konec datoteke
					} else
					{
						if (c == ',')
						{
							state = -1; // konec besede
						} else
						{
							word.append((char) c);
						}
					}
				}
					break;
				default:
					break;
			}
		}
		return word.toString();
	}
	
	public static void main(String[] args)
	{
		// TEST
		StopWordsExtract s= new StopWordsExtract(new File("data/common-english-words.txt"));
		System.out.println(s.isStopWord("OF"));
	}
	
}
